package com.lbn.jfqsc.app.message.performance;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.base.BaseFragment;
import com.lbn.baseframe.base.BaseRecyclerAdapter;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.message.adapter.ActionAdapter;
import com.lbn.jfqsc.app.message.adapter.DiscussAdapter;
import com.lbn.jfqsc.app.message.performance.detail.DetailActivity;
import com.lbn.jfqsc.app.message.adapter.PfmAdapter;
import com.lbn.jfqsc.business.bean.ActivityList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PfmFragment extends BaseFragment<PfmPresenter> implements IPfmView,
        BaseRecyclerAdapter.OnItemClickListener,XRecyclerView.LoadingListener {

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    private BaseRecyclerAdapter adapter;
    private int type;
    public static PfmFragment newInstance(int type) {
        
        Bundle args = new Bundle();
        args.putInt("type",type);
        PfmFragment fragment = new PfmFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_pfm, null, false);
            ButterKnife.bind(this, rootView);
        }
        return rootView;
    }

    @Override
    public PfmPresenter createPresenter() {
        return new PfmPresenter();
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();
        type = getArguments().getInt("type");
        if(type==1){
//            adapter = new DiscussAdapter(getContext(),null);
            adapter = new ActionAdapter(getContext(),null);
        }else{
            adapter = new PfmAdapter(getContext(),null);
        }
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(this);
        recyclerView.setRefreshing(true);
    }
    int page =1 ;
    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.activityList(page,type);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.activityList(page,type);
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        if(type==1){
            com
                    .lbn
                    .jfqsc
                    .app
                    .message
                    .action
                    .detail
                    .DetailActivity
                    .startActivity(getContext(), (ActivityList) adapter.getItem(pos-1));
        }else{
            DetailActivity.startActivity(getContext(),(ActivityList) adapter.getItem(pos-1));
        }
    }

    @Override
    public void isMore(boolean b) {
        recyclerView.setNoMore(!b);
    }

    @Override
    public void setData(List list) {
        adapter.setData(list);
        recyclerView.refreshComplete();
    }

    @Override
    public void addData(List list) {
        adapter.addAll(list);
        recyclerView.loadMoreComplete();
    }
}
